package br.atribuicoes.professor.producaoAcademica

class ProducaoAcademicaController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index = {
		redirect(action: "list", params: params)
	}

	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[producaoAcademicaInstanceList: ProducaoAcademica.list(params), producaoAcademicaInstanceTotal: ProducaoAcademica.count()]
	}
	def createProducao = {
		
		TipoAtividadeProducaoAcademica tipoAtividade = TipoAtividadeProducaoAcademica.get(params.tipoAtividade.id)
		println ("Tipo Atividade"+ tipoAtividade)
		String controller = tipoAtividade.grupoProducaoAcademica.classe.toString()
		
		redirect(controller:controller , action: "create")
	
	}
	def create = {
		def producaoAcademicaInstance = new ProducaoAcademica()
		producaoAcademicaInstance.properties = params

		List<TipoAtividadeProducaoAcademica> tipoAtividadeProducaoAcademica = TipoAtividadeProducaoAcademica.list()

		return [producaoAcademicaInstance: producaoAcademicaInstance, tipoAtividadeProducaoAcademica: tipoAtividadeProducaoAcademica]
	}

	def save = {
		def producaoAcademicaInstance = new ProducaoAcademica(params)
		if (producaoAcademicaInstance.save(flush: true)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'producaoAcademica.label', default: 'ProducaoAcademica'), producaoAcademicaInstance.id])}"
			redirect(action: "show", id: producaoAcademicaInstance.id)
		}
		else {
			render(view: "create", model: [producaoAcademicaInstance: producaoAcademicaInstance])
		}
	}

	def show = {
		def producaoAcademicaInstance = ProducaoAcademica.get(params.id)
		if (!producaoAcademicaInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'producaoAcademica.label', default: 'ProducaoAcademica'), params.id])}"
			redirect(action: "list")
		}
		else {
			[producaoAcademicaInstance: producaoAcademicaInstance]
		}
	}

	def edit = {
		def producaoAcademicaInstance = ProducaoAcademica.get(params.id)
		if (!producaoAcademicaInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'producaoAcademica.label', default: 'ProducaoAcademica'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [producaoAcademicaInstance: producaoAcademicaInstance]
		}
	}

	def update = {
		def producaoAcademicaInstance = ProducaoAcademica.get(params.id)
		if (producaoAcademicaInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (producaoAcademicaInstance.version > version) {

					producaoAcademicaInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'producaoAcademica.label', default: 'ProducaoAcademica')] as Object[], "Another user has updated this ProducaoAcademica while you were editing")
					render(view: "edit", model: [producaoAcademicaInstance: producaoAcademicaInstance])
					return
				}
			}
			producaoAcademicaInstance.properties = params
			if (!producaoAcademicaInstance.hasErrors() && producaoAcademicaInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'producaoAcademica.label', default: 'ProducaoAcademica'), producaoAcademicaInstance.id])}"
				redirect(action: "show", id: producaoAcademicaInstance.id)
			}
			else {
				render(view: "edit", model: [producaoAcademicaInstance: producaoAcademicaInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'producaoAcademica.label', default: 'ProducaoAcademica'), params.id])}"
			redirect(action: "list")
		}
	}

	def delete = {
		def producaoAcademicaInstance = ProducaoAcademica.get(params.id)
		if (producaoAcademicaInstance) {
			try {
				producaoAcademicaInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'producaoAcademica.label', default: 'ProducaoAcademica'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'producaoAcademica.label', default: 'ProducaoAcademica'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'producaoAcademica.label', default: 'ProducaoAcademica'), params.id])}"
			redirect(action: "list")
		}
	}
}
